import request from '@/utils/axios'
import { AppointmentInfo, QueryParams, ResponseData } from './types'
import type { AxiosResponse } from 'axios'

/**
 * 用于调用 API 接口的函数
 * @method AppointmentInfoApi 获取预约相关
 */

export default {
  // 获取预约列表或单个预约信息
  getAppointments(params?: QueryParams): Promise<AxiosResponse<ResponseData<AppointmentInfo[]>>> {
    return request({
      url: '/api/appointmentInfo',
      method: 'get',
      params
    })
  },

  // 获取单个预约详情
  getAppointment(recordId: string): Promise<AxiosResponse<ResponseData<AppointmentInfo>>> {
    return request({
      url: `/api/appointmentInfo/${recordId}`,
      method: 'get'
    })
  },

  // 创建单个预约
  createAppointment(data: Partial<AppointmentInfo>): Promise<AxiosResponse<ResponseData<AppointmentInfo>>> {
    return request({
      url: '/api/appointmentInfo',
      method: 'post',
      data
    })
  },

  // 批量创建预约
  createAppointments(appointmentList: Partial<AppointmentInfo>[]): Promise<AxiosResponse<ResponseData<AppointmentInfo>>> {
    return request({
      url: '/api/appointmentInfo',
      method: 'post',
      data: {
        AppointmentInfoList: appointmentList
      }
    })
  },

  // 更新预约信息
  updateAppointment(recordId: string, data: Partial<AppointmentInfo>): Promise<AxiosResponse<ResponseData<AppointmentInfo>>> {
    return request({
      url: `/api/appointmentInfo/${recordId}`,
      method: 'put',
      data
    })
  },

  // 敏感信息更新
  sensitiveUpdate(recordId: string, data: {
    aspect?: string
    appointment_status?: number
    is_delete?: number
  }): Promise<AxiosResponse<ResponseData<AppointmentInfo>>> {
    const formData = new FormData()
    Object.entries(data).forEach(([key, value]) => {
      if (value !== undefined) {
        formData.append(key, value.toString())
      }
    })
    return request({
      url: `/api/appointmentinfo/update/${recordId}`,
      method: 'put',
      data: formData
    })
  },

  // 删除预约
  deleteAppointment(recordId: string): Promise<AxiosResponse<ResponseData<any>>> {
    return request({
      url: `/api/appointmentInfo/${recordId}`,
      method: 'delete'
    })
  }
}